//
//  WCNTemplateListVC.m
//  WCN
//
//  Created by wsj2015 on 15/12/15.
//  Copyright © 2015年 ShanghaiCize Trade And Business Co., Ltd. All rights reserved.
//

#import "WCNTemplateListVC.h"
#import "WCNTTemplateListCell.h"
#import "WCButton.h"
#import "UINavigationItem+Offset.h"
#import "MJRefresh.h"
#import "WCNMeSetTimes(replace)VC.h"
#import "WCNetworkAccessManager.h"
#import "Macros.h"
#import "WCDefaultManager.h"
#import "WCPopupLayerManager.h"
#import "WCNTempetateModel.h"
@interface WCNTemplateListVC ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIButton *applicationTemplateBtn;
@property (weak, nonatomic) IBOutlet UIButton *editeBtn;

@end

@implementation WCNTemplateListVC
{
    NSMutableArray *_dataArray;
    WCNTempetateModel *_currnetModel;
    
    WCNTempetateModel *_toDelModel;


}
- (instancetype)init
{
    if (self = [super init]) {
        _dataArray =[NSMutableArray array];
    }
    return self;

}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"模板列表";
    WCButton *rightBtn = [WCButton wcButtonWithType:WCButtonTypeAdd];
    [rightBtn addTarget:self action:@selector(addTempetateAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.navigationItem addRightBarButtonItem:rightBtn];
    self.applicationTemplateBtn.layer.cornerRadius = 5;
    self.applicationTemplateBtn.layer.masksToBounds = YES;
    self.editeBtn.layer.cornerRadius = 5;
    self.editeBtn.layer.masksToBounds = YES;
    [self getTempetateListData];
   
    
}

- (void)getTempetateListData
{
     Account *accout =[[WCDefaultManager shareMg] account];
    MBProgressHUD *hud =[[WCPopupLayerManager shareMg] showSimple:self.view];
    NSDictionary *params =@{@"nurseId":accout.stringId};
    [[WCNetworkAccessManager sharedMg] POST:kserviceTimeTempetateList_prat parameters:params success:^(NSDictionary *responseObject) {
        [_dataArray removeAllObjects];
        for (NSDictionary *dataDict in responseObject[@"data"]) {
            WCNTempetateModel *model =[[WCNTempetateModel alloc] init];
            [model setValuesForKeysWithDictionary:dataDict];
            [_dataArray addObject:model];
        }
        [self loadNOrecodView:_tableView dataArray:_dataArray];
        [_tableView reloadData];
        _currnetModel = nil;
        
    } failure:nil netError:nil always:^{
        [hud hide: YES];
    }];
    


}

- (void)addTempetateAction:(id)sender
{
    WCNMeSetTimes_replace_VC *vc =[[WCNMeSetTimes_replace_VC alloc] init];
    weakSelf_define;
    
    vc.callBack =^(){
        
        [weakSelf getTempetateListData];
    };
    [self.navigationController pushViewController:vc animated:YES];
   

}
- (IBAction)editeTempatateAction:(id)sender {
    if (!_currnetModel) {
        [[WCPopupLayerManager shareMg] showTextOnly:@"请先选定一个模板" withView:self.view];
        return;
    }
    
    weakSelf_define;
    WCNMeSetTimes_replace_VC *vc =[[WCNMeSetTimes_replace_VC alloc] init];
    vc.tempelateId = [_currnetModel.templateId integerValue];
     vc.isEdit = YES;
    vc.callBack =^(){
        [weakSelf getTempetateListData];
    
    };
    
    [self.navigationController pushViewController:vc animated:YES];
}
- (IBAction)applyAction:(id)sender {
    
    
    if (!_currnetModel ) {
        [[WCPopupLayerManager shareMg] showTextOnly:@"请先选定一个模板" withView:self.view];
        return;
    }
    
//    "templateId": 1,
//    "nurseId": 1
   
    weakSelf_define;
    MBProgressHUD *hud =[[WCPopupLayerManager shareMg] showSimple:self.view];
    NSMutableDictionary *params =[NSMutableDictionary dictionary];
    params[@"templateId"] =_currnetModel.templateId;
    params[@"nurseId"] =_currnetModel.nurseId;
    [[WCNetworkAccessManager sharedMg] POST:kApplyTempelateUrl_part parameters:params success:^(NSDictionary *responseObject) {
        [[WCPopupLayerManager shareMg] showTextOnly:responseObject[@"data"] withView:[UIApplication sharedApplication].keyWindow];
        [weakSelf.navigationController popViewControllerAnimated:YES];
    } failure:nil netError:nil always:^{
        [hud hide:YES];
        
    }];
    
    
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    WCNTempetateModel *tempetateModel =_dataArray[indexPath.row];
    _currnetModel =tempetateModel;


}

- (UITableViewCellEditingStyle )tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return  UITableViewCellEditingStyleDelete;

}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    WCNTempetateModel *model =_dataArray[indexPath.row];
    _toDelModel = model;
    _toDelModel.currentPath = indexPath;
 
    if (editingStyle ==UITableViewCellEditingStyleDelete) {
        UIAlertView *alerView =[[UIAlertView alloc] initWithTitle:@"提示" message:@"是否删除该模板" delegate:self cancelButtonTitle:@"是" otherButtonTitles:@"否", nil];
       
        alerView.tag = 3001;
        [alerView show];
        
        
    }

}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag == 3001) {
        if (buttonIndex ==0) {
            weakSelf_define;
            MBProgressHUD *hud =[[WCPopupLayerManager shareMg]showSimple:self.view];
            [[WCNetworkAccessManager sharedMg] POST:kDeleteTempelateUrl_part parameters:@{@"id":_toDelModel.templateId} success:^(NSDictionary *responseObject) {
                
                [[WCPopupLayerManager shareMg] showTextOnly:responseObject[@"data"] withView:weakSelf.view];
                [_dataArray removeObjectAtIndex:_toDelModel.currentPath.row];
                [_tableView reloadData];
                _currnetModel = nil;
                
            } failure:nil netError:nil always:^{
                [hud hide:YES];
            }];

           
            
        }
    }
   
}

- (NSString*)tableView:(UITableView*)tableView
titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath*)indexpath {
    return @"删除";
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellId =@"templateCell";
    WCNTTemplateListCell *cell =[tableView dequeueReusableCellWithIdentifier:cellId];
    if (nil ==cell) {
        cell = [[NSBundle mainBundle] loadNibNamed:@"WCNTTemplateListCell" owner:nil options:0].lastObject;
    }
    cell.tempetateModel =_dataArray[indexPath.row];
    
    return cell;

}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 49;

}

@end
